package com.tianqu.tms.www.car.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.www.car.controller.CarTimeDateCheckInfoController;

/**
 * 挂起解挂临时进出时间设定
 * @author mufeiyu
 */
public class CarTimeDateCheckInfoAjaxAction extends TmsAjaxAction {
	private Logger logger = Logger.getLogger(CarTimeDateCheckInfoAjaxAction.class);
	public static final String STORAGEENTERDATE = "storageEnterDate";
	public static final String STORAGETIME = "storageTime";
	public static final String ENTERTIME = "enterTime";
	public static final String SERIAL = "serial";
	public static final String ISPUSH = "isPush";

	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String serial = request.getParameter(SERIAL);
		String storageEnterDate = request.getParameter(STORAGEENTERDATE);
		String storageTime = request.getParameter(STORAGETIME);
		
		String isPush = request.getParameter(ISPUSH);
		
		String dateErrorFlag = "";
		String timeErrorFlag = "";
		
		uc.setAttribute(SERIAL, serial);
		uc.setAttribute(STORAGEENTERDATE, storageEnterDate);
		uc.setAttribute(STORAGETIME, storageTime);

		uc.setAttribute(ISPUSH, isPush);
		
		try {
			//进出日期check
			SimpleDateFormat formatDate1 = new SimpleDateFormat("yyyy-MM-dd");
			// 当前系统日期
			String nowDateStr = formatDate1.format(new Date());
			Date nowDate = formatDate1.parse(nowDateStr);
			// 进出日期	
			Date endDate = formatDate1.parse(storageEnterDate);
			// 比较--进出日期日期没有比当前系统日期大：页面提示“【U0021】{0}只能填写今天以后的日期（包括今天在内）。”
			if (endDate.compareTo(nowDate) < 0) {
				dateErrorFlag = "1";
			}
		} catch (ParseException e1) {
			logger.error("",e1);
		}
		if(StringUtil.isNullOrBlank(dateErrorFlag) && StringUtil.isNullOrBlank(timeErrorFlag)){
			CarTimeDateCheckInfoController controller = new CarTimeDateCheckInfoController();
			try {
				controller.execute(uc);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		StringBuffer str = new StringBuffer();
		str.append(dateErrorFlag);
		str.append("##");
		str.append(timeErrorFlag);	
		
		String responseText = str.toString();

		responseValues(response, responseText, TEXT);
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
